﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Windows.Forms;
using SyncList.UserInterface.Helpers;

namespace SyncList.UserInterface
{
	/// <summary>
	/// Interaction logic for WinAddFolder.xaml
	/// </summary>
	public partial class WinAddFolder : Window
	{

        /// <summary>
        /// Valeur sélectionnée
        /// </summary>
        private string _selectedValue = null;

        /// <summary>
        /// Constructeur
        /// </summary>
		public WinAddFolder()
		{
			this.InitializeComponent();

            this.BtnAccept.Click += new RoutedEventHandler(BtnAccept_Click);
            this.BtnClose.Click += new RoutedEventHandler(BtnClose_Click);
            this.BtnSelectFolder.Click += new RoutedEventHandler(BtnSelectFolder_Click);
            this.ImageOpen.Fill = IconHelper.GetImageBrush("open_folder.png");
		}


        /// <summary>
        /// Valeur sélectionnée
        /// </summary>
        public string SelectedValue        
        {
            get { return (_selectedValue); }
            set { _selectedValue = value; }
        }



        /// <summary>
        /// Sélection d'un dossier
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnSelectFolder_Click(object sender, RoutedEventArgs e)
        {
            //GPI : pourquoi pas plutot un FolderBrowserDialog browser = new FolderBrowserDialog(); ?
            //PS : J'AI TESTE CA FONCTIONNE ;)

            FolderBrowserDialog browser = new FolderBrowserDialog();
            browser.ShowDialog();
            this.TextBoxPath.Text = browser.SelectedPath;

            //Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            //dialog.ShowDialog(this);
            //string path = dialog.FileName;

            //string finalPath = "";

            //if (!String.IsNullOrWhiteSpace(path))
            //{
            //    int pos = path.LastIndexOf(@"\");
            //    if (pos >= 0)
            //        path = path.Substring(0, pos + 1);
            //    finalPath = path;
            //}
            //else
            //{
            //    if (!String.IsNullOrWhiteSpace(dialog.InitialDirectory))
            //    {
            //        finalPath = dialog.InitialDirectory;

            //    }
            //}
            //this.TextBoxPath.Text = finalPath;
        }


        /// <summary>
        /// Fermer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnClose_Click(object sender, RoutedEventArgs e)
        {

            this.Close();
        }

        /// <summary>
        /// Accepted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnAccept_Click(object sender, RoutedEventArgs e)
        {
            string path = this.TextBoxPath.Text;
            if (!String.IsNullOrWhiteSpace(path))
            {
                if (Directory.Exists(path))
                {
                    SelectedValue = path;
                    this.Close();
                }
            }
        }
	}
}